package bar.dark.lee.alarm.effect.buttonrush;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import bar.dark.lee.alarm.R;
import bar.dark.lee.alarm.effect.EffectActivity;
import bar.dark.lee.alarm.effect.EffectListActivity;
import bar.dark.lee.alarm.effect.question.QuestionResultDialog;

public class EffectButtonRushActivity extends EffectActivity {

	private int effectNumber;
	private boolean flagTestReal;
	
	private TextView textCorrect;
	private Button btnSend;
	private ProgressBar progressGage;
	
	private int result;
	private int correctCount = 100;
	
	private boolean flagProgress = true;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.effectbuttonrush_activity);
		
		Intent intent = getIntent();
		effectNumber = intent.getIntExtra(EffectListActivity.EFFECT_PLAY, -1);
		flagTestReal = intent.getBooleanExtra(EffectActivity.FLAG_TEST_REAL, false);
		
		if (flagTestReal)
		{
			TextView leftButton = (TextView) findViewById(R.id.superLeftButton);
			leftButton.setVisibility(View.INVISIBLE);
		}
		
		if (effectNumber == -1)
		{
			
		}
		else
		{
			super.setTitle(effectNumber);
		}
		
		setFindView();
		setDataSetting();
		setClickListener();
		
		startProgressHandler();
		
	}
	
	@Override
	public void setFindView() {
		
		textCorrect = (TextView) findViewById(R.id.effectButtonRush_textCorrect);
		btnSend = (Button) findViewById(R.id.effectButtonRush_btnSend);
		progressGage = (ProgressBar) findViewById(R.id.effectButtonRush_progress);
		
	}

	@Override
	public void setDataSetting() {
		
		textCorrect.setText(0+"%");
		progressGage.setMax(correctCount);
		progressGage.setProgress(0);
		
	}

	@Override
	public void setClickListener() {
		
		btnSend.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				int currentProgress = progressGage.getProgress();
				int resultProgress = currentProgress + 2;
				
				if (flagProgress)
				{
					if (resultProgress >= 99)
					{
						flagProgress = false;
						
						progressGage.setProgress(100);
						textCorrect.setText("100%");
						
						playResult(true);
					}
					else
					{
						if (currentProgress <= 0)
						{
							progressGage.setProgress(2);
							textCorrect.setText("2%");
						}
						else
						{
							progressGage.setProgress(resultProgress);
							textCorrect.setText(resultProgress+"%");
						}
					}
				}
			}
		});
		
	}

	@Override
	protected void tipButton() {
	}
	
	private Handler handler = new Handler()
	{
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			
			int currentProgress = progressGage.getProgress();

			if (currentProgress > 99)
			{
			}
			else
			{
				if (flagProgress)
				{
					if (currentProgress <= 0)
					{
						progressGage.setProgress(0);
						textCorrect.setText("0%");
					}
					else
					{
						progressGage.setProgress(currentProgress - 1);
						textCorrect.setText((currentProgress - 1)+"%");
					}
					
					startProgressHandler();
				}
				else
				{
					progressGage.setProgress(100);
					textCorrect.setText("100%");
				}
			}
		}
	};
	
	private void startProgressHandler()
	{
		handler.sendEmptyMessageDelayed(0, 5 * 100);
	}
	
	private void playResult(boolean result)
	{
		Intent intent = new Intent(superContext, QuestionResultDialog.class);
		intent.putExtra(QuestionResultDialog.QUESTION_RESULT, result);
		startActivityForResult(intent, QuestionResultDialog.REQUEST_BUTTONRUSH);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if (requestCode == QuestionResultDialog.REQUEST_BUTTONRUSH)
		{
			if (resultCode == RESULT_OK)
			{
				finish();
			}
		}
	}
}
